package com.sms.web.system.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sms.common.exception.SystemException;
import com.sms.common.persistence.DynamicSpecifications;
import com.sms.common.persistence.Operator;
import com.sms.common.persistence.SearchFilter;
import com.sms.web.system.dao.SysRoleDao;
import com.sms.web.system.dao.SysRoleMenuDao;
import com.sms.web.system.dao.SysUserRoleDao;
import com.sms.web.system.entity.SysRole;
import com.sms.web.system.entity.SysRoleMenu;
import com.sms.web.system.entity.SysUserRole;

/**
 * @desc 
 * @author wjw
 * @time 2017年11月16日下午3:34:16
 */
@Service
@Transactional
public class SysRoleService {
    
    protected Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    private SysRoleDao roleDao;
    @Autowired
    private SysUserRoleDao urDao;
    @Autowired
    private SysRoleMenuDao rmDao;
    
    /** 保存角色 */
    public SysRole saveRole(SysRole entity) {
        return roleDao.save(entity);
    }
    
    /** 保存角色菜单 */
    public void saveRoleMenu(String roleId, String menuIds) {
    	//1.删除旧的角色菜单
    	rmDao.deleteInBatch(rmDao.findByRoleId(roleId));
    	//2.增加新的角色菜单
    	List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
    	for(String id : menuIds.split(",")) {
    		SysRoleMenu item = new SysRoleMenu(roleId, id);
    		list.add(item);
    	}
    	rmDao.saveAll(list);
    }
    
    /**
     * 删除角色
     * @param id
     * @throws SystemException 
     */
    public void delete(String id) throws SystemException {
    	//1.删除角色前进行判断
    	List<SysUserRole> list = urDao.findByRoleId(id);
    	if( !list.isEmpty() ) {
    		throw new SystemException("该角色下有用户");
    	}
    	//2.删除角色菜单
    	rmDao.deleteInBatch(rmDao.findByRoleId(id));
    	//3.删除角色实体
    	roleDao.deleteById(id);
    }
    
    /** 查询角色 */
    public SysRole findById(String id) {
        return roleDao.getOne(id);
    }
    
    /**
     * 分页查询角色
     * @param serchkey
     * @param pageNumber
     * @param pageSize
     * @param sort 排序条件
     * @return
     */
    @Transactional(readOnly = true)
    public Page<SysRole> findByKeyword(String serchkey, int pageNumber, int pageSize) {
    	//1.定义jpa分页对象
    	Sort sort = new Sort(Direction.DESC, "id");
        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize, sort);
        //2.构建动态查询条件
        Collection<SearchFilter> filters = new ArrayList<>();
        filters.add(new SearchFilter("roleName", Operator.LIKE , serchkey));//模糊匹配角色名
        filters.add(new SearchFilter("delFlag", Operator.EQ , "0"));//查询未删除数据
        Specification<SysRole> spec = DynamicSpecifications.bySearchFilter(filters, SysRole.class);
        return roleDao.findAll(spec, pageRequest);
    }
}
